package cn.com.dealme.domain.model;

import cn.com.dealme.domain.model.exception.CommodityException;
import cn.com.dealme.domain.vo.CommodityTradeStateVO;
import cn.com.dealme.domain.vo.CommodityVO;

public class Commodity {

	private Long id;
	
	private CommodityVO commodityInfo;
	
	private Catagory catagory;
	
	private CommodityTradeStateVO tradeState;
	
	private User user;
	
	/** constructors **/
	public Commodity() {
	}
	
	public Commodity(Long id) {
		this.id = id;
	}
	
	public Commodity(CommodityVO commodityInfo, Catagory catagory, CommodityTradeStateVO tradeState, User user) {
		if(null == commodityInfo) {
			throw new CommodityException(CommodityException.INFO_NULL_EXCEPTION);
		}
		if(null == catagory) {
			throw new CommodityException(CommodityException.CATAGORY_NULL_EXCEPTION);
		}
		if(null == tradeState) {
			throw new CommodityException(CommodityException.TRADE_STATE_NULL_EXCEPTION);
		}
		if(null == user) {
			throw new CommodityException(CommodityException.USER_NULL_EXCEPTION);
		}
		this.catagory = catagory;
		this.commodityInfo = commodityInfo;
		this.tradeState = tradeState;
	}
	
	/** action **/
	public void updateCommodityInfo(CommodityVO commodityInfo) {
		if(null == commodityInfo) {
			throw new CommodityException(CommodityException.INFO_NULL_EXCEPTION);
		}
		this.commodityInfo = commodityInfo;
		this.commodityInfo.setId(this.getId());
	}
	
	public void updateCatagory(Catagory catagory) {
		if(null == catagory) {
			throw new CommodityException(CommodityException.CATAGORY_NULL_EXCEPTION);
		}
		this.catagory = catagory;
	}
	
	public void updateTradeState(CommodityTradeStateVO tradeState) {
		this.tradeState = tradeState;
		this.tradeState.setId(this.getId());
	}

	
	/** getters and setters **/
	public CommodityVO getCommodityInfo() {
		return commodityInfo;
	}

	protected Catagory getCatagory() {
		return catagory;
	}

	public CommodityTradeStateVO getTradeState() {
		return tradeState;
	}

	protected void setCommodityInfo(CommodityVO commodityInfo) {
		this.commodityInfo = commodityInfo;
	}

	protected void setCatagory(Catagory catagory) {
		this.catagory = catagory;
	}

	protected void setTradeState(CommodityTradeStateVO tradeState) {
		this.tradeState = tradeState;
	}

	public Long getId() {
		return id;
	}

	protected void setId(Long id) {
		this.id = id;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(null == obj) return false;
		if(!(obj instanceof Commodity)) return false;
		if(this.id.equals(((Commodity)obj).id)) {
			return true;
		}
		return false;
	}

	@Override
	public int hashCode() {
		return this.id.hashCode();
	}

	public User getUser() {
		return user;
	}

	protected void setUser(User user) {
		this.user = user;
	}
	
}
